"""
投票
"""
from fastapi import APIRouter, Depends, Query, Response
from sqlalchemy.orm import Session

from app.dependencies import get_db
from schemas import (
    Vote,
    VoteCreate,
    VoteAdjust,
    VoteDelete,
    Participant,
    ParticipantCreate,
    ParticipantAdjust,
    ParticipantDelete
)
from sql_app.crud import crud_vote, crud_vote_participant
from utils import await_task, resp_200, resp_detail_200

router = APIRouter(
    prefix="/vote",
    tags=["vote"],
    responses={404: {"description": "Not found"}}
)


@router.post("/add_category")
async def add_category(
    vote: VoteCreate, db: Session=Depends(get_db)
):
    await await_task(crud_vote.create, db, vote)
    return resp_200()


@router.get("/get_category_list")
async def get_category_list(
        accesstoken: str = Query(..., example="TOKEN3590"),
        pageidx: int = Query(..., example = 1),
        pagesize: int = Query(..., example = 20), keyword: str = None,
        db: Session = Depends(get_db)):
    total = await await_task(crud_vote.get_count, db)
    data = await await_task(
        crud_vote.get_a_batch, db, (pageidx-1)*pagesize, pagesize, keyword
    )
    print(Vote.from_orm(data[0]))
    return resp_200(total=total, data=[Vote.from_orm(i) for i in data])


@router.post("/delete_category")
async def delete_category(
        activity: VoteDelete, db: Session = Depends(get_db)):
    await await_task(crud_vote.delete, db, activity)
    return resp_200()


@router.get("/get_category_info")
async def get_category_info(
        accesstoken: str=Query(..., example="TOKEN3590"),
        id: int = Query(..., example=1),
        db: Session = Depends(get_db)
):
    data = await await_task(
        crud_vote.get_detail, db, id)

    return resp_detail_200(data=Vote.from_orm(data))


@router.post("/adjust_category")
async def adjust_category(
        vote: VoteAdjust, db: Session = Depends(get_db)):
    await await_task(crud_vote.adjust, db, vote)
    return resp_200()


@router.get("/get_participant_list")
async def get_participant_list(
        accesstoken: str = Query(..., example="TOKEN3590"),
        pageidx: int = Query(..., example = 1),
        pagesize: int = Query(..., example = 20), keyword: str = None,
        db: Session = Depends(get_db)):
    total = await await_task(crud_vote.get_count, db)
    data = await await_task(
        crud_vote_participant.get_a_batch, db, (pageidx-1)*pagesize, pagesize, keyword
    )

    return resp_200(total=total, data=[Participant.from_orm(i) for i in data])


@router.post("/add_participant")
async def add_participant(
    participant: ParticipantCreate, db: Session=Depends(get_db)
):
    await await_task(crud_vote_participant.create, db, participant)
    return resp_200()


@router.post("/adjust_participant")
async def adjust_participant(
        participant: ParticipantAdjust, db: Session = Depends(get_db)):
    await await_task(crud_vote_participant.adjust, db, participant)
    return resp_200()


@router.post("/delete_participant")
async def delete_participant(
        participant: ParticipantDelete, db: Session = Depends(get_db)):
    await await_task(crud_vote_participant.delete, db, participant)
    return resp_200()


@router.get("/get_participant_info")
async def get_participant_info(
        accesstoken: str=Query(..., example="TOKEN3590"),
        id: int = Query(..., example=1),
        db: Session = Depends(get_db)
):
    data = await await_task(
        crud_vote_participant.get_detail, db, id)

    return resp_detail_200(data=Participant.from_orm(data))
